local qianche = fk.CreateSkill{
  name = "hanqing__qianche",
}

Fk:loadTranslationTable{
  ["hanqing__qianche"] = "谦澈",
  [":hanqing__qianche"] = "出牌阶段，或当你受到伤害后，你可以（通过明置或弃置）将明置手牌数调整至体力值，然后若你的手牌均明置，"..
  "你将手牌摸至手牌上限。你的黑色明置牌不计入手牌上限。",

  ["#hanqing__qianche"] = "谦澈：选择弃置任意张手牌，然后明置任意张手牌，将明置手牌数调整至体力值",
  ["#hanqing__qianche-show"] = "谦澈：选择要明置的%arg张手牌",
}

local DIY = require "packages/diy_utility/diy_utility"

qianche:addEffect("active", {
  anim_type = "support",
  prompt = "#hanqing__qianche",
  target_num = 0,
  card_filter = function (self, player, to_select, selected)
    return table.contains(player:getCardIds("h"), to_select) and not player:prohibitDiscard(to_select)
  end,
  feasible = function (self, player, selected, selected_cards, card)
    return player:getHandcardNum() - #selected_cards >= player.hp
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local n = player.hp - #table.filter(DIY.getShownCards(player), function (id)
      return not table.contains(effect.cards, id)
    end)
    if n > 0 then
      local cards = table.filter(player:getCardIds("h"), function (id)
        return not table.contains(DIY.getShownCards(player), id) and not table.contains(effect.cards, id)
      end)
      if #cards > n then
        cards = room:askToCards(player, {
          min_num = n,
          max_num = n,
          include_equip = false,
          skill_name = qianche.name,
          pattern = tostring(Exppattern{ id = cards }),
          prompt = "#hanqing__qianche-show:::"..n,
          cancelable = false,
        })
      end
      DIY.showCards(player, cards)
    end
    if #effect.cards > 0 then
      room:throwCard(effect.cards, qianche.name, player, player)
    end
    if player.dead then return end
    if player:getHandcardNum() == #DIY.getShownCards(player) then
      n = 0
      if player:hasSkill(self) then
        n = player:getMaxCards() - player:getHandcardNum() +
          #table.filter(DIY.getShownCards(player), function (id)
            return Fk:getCardById(id).color == Card.Black
          end)
      else
        n = player:getMaxCards() - player:getHandcardNum()
      end
      if n > 0 then
        player:drawCards(n, qianche.name)
      end
    end
  end,
})

qianche:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(qianche.name) and
      player:getHandcardNum() >= player.hp
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local success, dat = room:askToUseActiveSkill(player, {
      skill_name = qianche.name,
      prompt = "#hanqing__qianche",
      cancelable = true,
      skip = true,
    })
    if success and dat then
      event:setCostData(self, {cards = dat.cards})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local skill = Fk.skills[qianche.name]
    skill:onUse(player.room, {
      from = player,
      cards = event:getCostData(self).cards,
    })
  end,
})

qianche:addEffect("maxcards", {
  exclude_from = function(self, player, card)
    return player:hasSkill(qianche.name) and table.contains(DIY.getShownCards(player), card.id) and card.color == Card.Black
  end,
})

return qianche
